package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.ContactsService;
import com.hnqcgczy.supplier.service.impl.ContactsServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Contacts;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ContactsServlet", value = "/ContactsServlet")
public class ContactsServlet extends BaseServlet {
    private ContactsService contactsService = new ContactsServiceImpl();


    public void addContacts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Contacts contacts = new Contacts();
        //获取值的操作
        contacts.setCompanyId(request.getParameter("company_id") != null ? Integer.parseInt(request.getParameter("company_id")) : 0);
        contacts.setName(request.getParameter("name"));
        contacts.setPosition(request.getParameter("position"));
        contacts.setPhone(request.getParameter("phone"));
        contacts.setTelephone(request.getParameter("telephone"));
        contacts.setEmail(request.getParameter("email"));
        boolean flag = contactsService.add(contacts);
        response.sendRedirect("ContactsServlet");

    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String name = request.getParameter("name") != null ? request.getParameter("name") : "";
        String phone = request.getParameter("phone") != null ? request.getParameter("phone") : "";
        Contacts query = new Contacts();
        query.setName(name);
        query.setPhone(phone);


        int pageNow = request.getParameter("pageNow") != null ? Integer.parseInt(request.getParameter("pageNow")) : 1;
        int pageSize = 10;

        int totals = contactsService.totals(query);
        if (pageNow <= 1) {
            pageNow = 1;
        }
        if (pageNow >= totals) {
            pageNow = totals;
        }
        List<Contacts> contacts = contactsService.findByLike(query, pageNow, pageSize);
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        //存
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("contacts", contacts);
        request.setAttribute("totals", totals);
        request.setAttribute("pages", pages);
        //跳
        request.getRequestDispatcher("ContactInfo.jsp").forward(request, response);
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Contacts contacts = new Contacts();
        //获取值的操作
        contacts.setContactsId(request.getParameter("contacts_id") != null ? Integer.parseInt(request.getParameter("contacts_id")) : 0);
        contacts.setCompanyId(request.getParameter("company_id") != null ? Integer.parseInt(request.getParameter("company_id")) : 0);
        contacts.setName(request.getParameter("name"));
        contacts.setPosition(request.getParameter("position"));
        contacts.setPhone(request.getParameter("phone"));
        contacts.setTelephone(request.getParameter("telephone"));
        contacts.setEmail(request.getParameter("email"));
        boolean flag = contactsService.update(contacts);
        response.sendRedirect("ContactsServlet");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        int contacts_id = request.getParameter("contacts_id") != null ? Integer.parseInt(request.getParameter("contacts_id")) : 0;
        boolean flag = contactsService.delete(contacts_id);
        response.sendRedirect("ContactsServlet");
    }
}
//postman测试请同学们自行完成